package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.dao.Setmeal_CheckGroupDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author 32338
 * @date 2024-05-03 20:13:07
 * @description 套餐服务层
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private Setmeal_CheckGroupDao setmeal_checkGroupDao;


    /**
     * @description
     ***:根据套餐id查询关联的检查组Id
     **/
    public List<Integer> findGroupIdBySetmealId(Integer setmealId) {

       return setmealDao.findGroupIdBySetmealId(setmealId);

    }

    /**
     * @description
     ***:新增套餐
     **/
    @Transactional
    public void add(Setmeal setmeal, List<Integer> checkgroupIds) {


        setmealDao.add(setmeal);
        Integer id = setmeal.getId();
        //测试组和测试项通过关联表关联;
        if(checkgroupIds!=null&&checkgroupIds.size()>0){
            setmeal_checkGroupDao.insertBatch(id,checkgroupIds);
        }

    }

   /**
    * @description
    ***:删除套餐
    **/
    @Transactional
    public void deleteById(Integer id) {
        //带有外键先删除关联表里面的
        setmeal_checkGroupDao.deleteById(id);
        setmealDao.deleteById(id);

    }

   /**
    * @description
    ***:修改套餐
    **/
    @Transactional
    public void edit(Setmeal setmeal, List<Integer> checkGroupIds) {
        //同样也要操作关联表
        setmealDao.edit(setmeal);
        //关联表先删除所有在添加实质上是
        Integer id = setmeal.getId();
        setmeal_checkGroupDao.deleteById(id);
        setmeal_checkGroupDao.insertBatch(id,checkGroupIds);
    }

   /**
    * @description
    ***:根据id查询套餐
    **/
    public Setmeal findById(Integer id) {
        Setmeal setmeal=setmealDao.findById(id);
        return setmeal;
    }

   /**
    * @description
    ***:分页查询
    **/
    public PageResult page(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page=setmealDao.page(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * @description
     ***:根据检查项或者检查组分页
     **/
    public List<? extends Serializable> findItemOrGroup(String type, String value) {
        if(type.equals("项目组")){
            List<CheckGroup> list=checkGroupDao.page(value);
            return list;
        }
        else if (type.equals("项目")){
            List<CheckItem> list=checkItemDao.findByCondition(value);
            return list;
        }

        return null;
    }
}
